#pragma once

#include <cstddef>
#include <cstring>
#include <stdexcept>

class SimpleString {
  size_t maxSize_;
  char *buffer_;
  size_t length_;

public:
  SimpleString(size_t maxSize) : maxSize_{maxSize}, length_{0} {
    if (maxSize == 0) {
      throw std::runtime_error("maxSize cannot be 0");
    }
    buffer_ = new char[maxSize]{0};
    buffer_[0] = 0;
  }
  ~SimpleString() { delete[] buffer_; }

public:
  void print(const char *tag) const { printf("%s: %s\n", tag, buffer_); }
  bool appendLine(const char *line) {
    const auto lineLength = strlen(line);
    if (length_ + lineLength + 2 > maxSize_) {
      return false;
    }
    std::strncpy(buffer_ + length_, line, maxSize_ - length_);
    length_ += lineLength;
    buffer_[length_++] = '\n';
    buffer_[length_] = 0;
    return true;
  }
};
